@echo off
chcp 65001 >nul
cls
color 0E

echo.
echo ════════════════════════════════════════════
echo   验证当前模拟位置状态
echo ════════════════════════════════════════════
echo.

rem 选择设备
set DEVICE=
for /f "skip=1 tokens=1" %%i in ('adb devices 2^>nul ^| find "device"') do (
    if not defined DEVICE set DEVICE=%%i
)

if "%DEVICE%"=="emulator-5554" (
    set DEVICE=-s emulator-5554
) else if "%DEVICE%"=="4f6c2e31" (
    set DEVICE=-s 4f6c2e31
) else (
    set DEVICE=
)

echo [1/4] 检查Mock Provider状态...
echo.

adb %DEVICE% shell dumpsys location | findstr /C:"mock" /C:"test" > temp_status.txt

type temp_status.txt | findstr /C:"provider" /C:"override"

echo.
echo ════════════════════════════════════════════
echo  分析结果
echo ════════════════════════════════════════════
echo.

findstr /C:"added mock" temp_status.txt >nul 2>&1
if not errorlevel 1 (
    echo ✅ Mock Provider 当前已添加
) else (
    echo ⚠️ Mock Provider 可能已被移除
)

findstr /C:"removed mock" temp_status.txt >nul 2>&1
if not errorlevel 1 (
    echo ⚠️ 检测到最近有移除操作
)

echo.

echo [2/4] 检查当前位置...
echo.

adb %DEVICE% shell dumpsys location | findstr /C:"last location" | findstr /C:"gps\|network" | findstr /C:"mock"

echo.

echo [3/4] 检查前台服务...
echo.

adb %DEVICE% logcat -d -s LocationForegroundService:D | tail -n 10 | findstr /C:"更新" /C:"位置"

echo.

echo [4/4] 检查应用状态...
echo.

adb %DEVICE% logcat -d -s LocationService:D | tail -n 10 | findstr /C:"模拟" /C:"Mock"

del temp_status.txt 2>nul

echo.
echo ════════════════════════════════════════════
echo  建议操作
echo ════════════════════════════════════════════
echo.

echo 如果Mock Provider被移除了：
echo.
echo   1. 在APP中重新设置模拟位置
echo      → 搜索地址或拖动地图
echo      → 点击确认
echo.
echo   2. 或者运行修复脚本：
echo      → 一键修复模拟位置.bat
echo.

pause

echo.
echo 是否需要重新设置模拟位置？
echo.
echo   1. 是 - 启动APP并监控
echo   2. 否 - 退出
echo.

set /p rechoice=请选择 (1/2): 

if "%rechoice%"=="1" (
    echo.
    echo 启动应用...
    adb %DEVICE% shell am start -n com.example.munidingwei/.MainActivity
    echo.
    echo 启动实时监控...
    timeout /t 2 >nul
    
    echo.
    echo 📱 请在手机上设置模拟位置
    echo.
    
    adb %DEVICE% logcat -c
    adb %DEVICE% logcat LocationService:D LocationForegroundService:D MockLocationProvider:D *:S
)

pause

